package com.liu.stockmarket.mpchart

import android.content.Context
import android.graphics.Canvas
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.renderer.YAxisRenderer
import com.github.mikephil.charting.utils.Transformer
import com.github.mikephil.charting.utils.ViewPortHandler
import com.liu.stockmarket.R

class MyYAxisRenderer(
    private val context: Context,
    viewPortHandler: ViewPortHandler,
    yAxis: YAxis,
    trans: Transformer
) :
    YAxisRenderer(viewPortHandler, yAxis, trans) {
    override fun drawYLabels(
        c: Canvas?,
        fixedPosition: Float,
        positions: FloatArray?,
        offset: Float
    ) {
        val from = if (mYAxis.isDrawBottomYLabelEntryEnabled) 0 else 1
        val to =
            if (mYAxis.isDrawTopYLabelEntryEnabled) mYAxis.mEntryCount else mYAxis.mEntryCount - 1

        // draw
        val upColor = context.getColor(R.color.up)
        val downColor = context.getColor(R.color.down)


        // y轴刻度按涨跌绘制颜色
        for (i in from until to) {
            val text = mYAxis.getFormattedLabel(i)

            if (text.startsWith("-")) {
                mAxisLabelPaint.color = downColor
            } else {
                mAxisLabelPaint.color = upColor
            }


//                    mAxisLabelPaint.color = mYAxis.textColor
            c!!.drawText(
                text,
                fixedPosition,
                positions!![i * 2 + 1] + offset,
                mAxisLabelPaint
            )
        }
    }
}